@extends("layout/main")
@section("content")
    <div class="col-sm-8 blog-main">
        <div class="blog-post">
            <div style="display:inline-flex">
                <h2 class="blog-post-title">{{$post->title}}</h2>
                @can('update',$post)
                    <a style="margin: auto" href="/posts/{{$post->id}}/edit">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                    </a>
                @endcan


                @can('delete',$post)
                    <a style="margin: auto" href="javascript:void(0)" onclick="del({{$post->id}})">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </a>
                @endcan
            </div>

            <p class="blog-post-meta">{{$post->created_at}} by <a href="#">{{$post->user->nickname}}</a></p>

            <p>{!!$post->content !!}<p>
            <div>
                @if($post->praised)
                    <a href="/posts/{{$post->id}}/zan" type="button" class="btn btn-default btn-lg">取消赞</a>
                @else
                    <a href="/posts/{{$post->id}}/zan" type="button" class="btn btn-primary btn-lg">赞</a>
                @endif
            </div>
        </div>

        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading">评论 共{{$post->comments_count}}条评论</div>

            <!-- List group -->
            <ul class="list-group">
                @foreach($post->comments as $comment)
                    <li class="list-group-item">
                        <h5>{{$comment->created_at}} by {{$comment->user->nickname}}</h5>
                        <div>
                            {{$comment->content}}
                        </div>
                    </li>
                @endforeach
            </ul>
        </div>

        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading">发表评论</div>

            <!-- List group -->
            <ul class="list-group">
                <form onsubmit="return false" id="formAdd">
                    {{csrf_field()}}
                    <li class="list-group-item">
                        <input type="hidden" name="post_id" value="{{$post->id}}">
                        <textarea name="content" class="form-control" rows="10"></textarea>
                        <button class="btn btn-default" type="submit" onclick="comment()">提交</button>
                    </li>
                </form>

            </ul>
        </div>

    </div><!-- /.blog-main -->
    <script>
        function comment() {
            $.ajax({
                url: '/posts/comment',
                type: "POST",
                data: new FormData($('#formAdd')[0]),
                contentType: false,
                processData: false,
                dataType: 'Json',
                success: function (data) {
                    if (data.code == 0) {
                        swalreload(data.message);
                    } else {
                        if (data.data.err_code) {
                            swalreload(data.data.msg, '/login');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                },
                error: function () {
                    swal("链接超时", '', 'error');
                }
            })
        }

        function del(id) {
            swal({
                title: '确定删除？',
                text: '删除后将找不回数据',
                type: 'info',
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: false
            }, function () {
                $.ajax({
                    url: "/posts/" + id + "/delete",
                    type: "POST",
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{csrf_token()}}"
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            swalreload('删除成功', '/posts');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    },
                    error: function () {
                        swal('请求超时，稍后再试', '', 'error');
                    }
                })
            });

        }
    </script>
@stop



